home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 013 / ov102.arc / SOURCE.ARC / OVTAG.C < prev   
Encoding:
C/C++ Source or Header  |  1987-02-14  |  13.5 KB  |  444 lines

  1. /*  019  14-Feb-87  ovtag.c
  2.  
  3.         Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
  4. */
  5.  
  6. #include "ov.h"
  7.  
  8. #ifndef NULL
  9. #define NULL (0)
  10. #endif
  11.  
  12. static char *name;                     /* wildcard (maybe) name to tag */
  13. static unsigned char attrib;           /* attribute to tag by */
  14. static unsigned from_date, to_date;    /* dates to use in tag by date */
  15. static unsigned from_time, to_time;    /* times to use in tag by time */
  16.  
  17. #define TDROWS 10                      /* box constants used by tag_date */
  18. #define TDCOLS 32
  19. #define TDFROW (FIRST_NROW+3)
  20. #define TDFCOL 24
  21.  
  22. static struct {        /* defines fields used by tag by date/time */
  23.    char row;
  24.    char col;
  25.    char len;
  26.    char value[9];
  27. } tdflds[] = { { TDFROW+4, TDFCOL+7, 8, "" },
  28. { TDFROW+4, TDFCOL+16, 8, "00:00:00" }, { TDFROW+4, TDFCOL+27, 1, "A" },
  29. { TDFROW+6, TDFCOL+7, 8, "" }, { TDFROW+6, TDFCOL+16, 8, "12:00:00" },
  30. { TDFROW+6, TDFCOL+27, 1, "P" } };
  31.  
  32. unsigned int cvtdate(), cvttime();
  33.  
  34. static struct {                /* used to check/convert time/date fields */
  35.    int *cvtresult;
  36.    int (*cvtrtn)();
  37.    char *p1, *p2;
  38.    char *name;
  39. } cvtflds[] = {
  40.    { &from_date, cvtdate, tdflds[0].value, NULL, "from date" },
  41.    { &to_date, cvtdate, tdflds[3].value, NULL, "to date" },
  42.    { &from_time, cvttime, tdflds[1].value, tdflds[2].value, "from time" },
  43.    { &to_time, cvttime, tdflds[4].value, tdflds[5].value, "to time" } };
  44.  
  45. int by_name(), by_date(), by_attrib();
  46.  
  47. extern struct window cw;
  48. extern struct file_ent files[];
  49.  
  50. unsigned int getdate();
  51. char *strupr(), *mmddyy();
  52.  
  53.  
  54. /******************************************************************************
  55.  **                       T A G _ C U R R E N T                              **
  56.  *****************************************************************************/
  57.  
  58. tag_current() {        /* toggle the tag state of the current file */
  59.  
  60.    tag_toggle(&files[cw.curidx],cw.curidx);  /* toggle the current file */
  61.  
  62.    disp_file_stats();                  /* display (maybe) updated file stats */
  63. }
  64.  
  65.  
  66. /******************************************************************************
  67.  **                      T A G _ M O D I F I E D                             **
  68.  *****************************************************************************/
  69.  
  70. tag_modified() {       /* tag files with the archive attriute set */
  71.  
  72.    attrib = ARCHIVE;                   /* tag files with this attribute */
  73.  
  74.    tag_files(by_attrib);               /* tag'em an' flag'em */
  75. }
  76.  
  77.  
  78. /******************************************************************************
  79.  **                         T A G _ N A M E                                  **
  80.  *****************************************************************************/
  81.  
  82. tag_name() {           /* wildcard tag of files by name */
  83.  
  84.    name = strupr(prompt("Tag by name","File name pattern: ",
  85.           NULL,0,12));
  86.  
  87.    if (strlen(name) == 0)
  88.       return;
  89.  
  90.    tag_files(by_name);                         /* tag all matchting files */
  91. }
  92.  
  93.  
  94. /******************************************************************************
  95.  **                         T A G _ D A T E                                  **
  96.  *****************************************************************************/
  97.  
  98. tag_date() {
  99.  
  100.    int cvterr;
  101.    register int i;
  102.    char *tmp, *boxsave;
  103.    unsigned char quit = 0;
  104.  
  105.    /* display the tag by date dialog box */
  106.  
  107.    boxsave = (char *) Malloc(TDROWS * TDCOLS * 2);
  108.  
  109.    setvattrib(DIS_BOX);
  110.    popup(TDFROW,TDFCOL,TDROWS,TDCOLS,boxsave);
  111.  
  112.    disp_str_at(" Tag by Date/Time ",TDFROW,TDFCOL+2);  /* display box title */
  113.  
  114.    disp_str_at("MM/DD/YY HH:MM:SS Am/Pm",TDFROW+2,TDFCOL+7);   /* box text */
  115.    disp_str_at("From",TDFROW+4,TDFCOL+2);
  116.    disp_str_at("To",TDFROW+6,TDFCOL+2);
  117.  
  118.    setvattrib(DIS_HIBOX);
  119.    disp_str_at("Press ESC to Quit",TDFROW+8,TDFCOL+7);
  120.  
  121.    if (strlen(tdflds[0].value) == 0) {         /* default to todays date */
  122.       tmp = mmddyy(getdate());
  123.       strcpy(tdflds[0].value,tmp);
  124.       strcpy(tdflds[3].value,tmp);
  125.    }
  126.  
  127.    /* display the input fields and default values */
  128.  
  129.    for (i = 0; i < 6; i++)
  130.       disp_str_at(tdflds[i].value,tdflds[i].row,tdflds[i].col);
  131.  
  132.    /* read the field values from user */
  133.  
  134.    for (i = 0; i < 6; i++) {
  135.       gotorc(tdflds[i].row,tdflds[i].col);
  136.       if (strlen(tmp = read_str(tdflds[i].len,tdflds[i].value,0)))
  137.          strcpy(tdflds[i].value,tmp);
  138.  
  139.       else {                   /* quit if nothing read */
  140.          quit = 1;
  141.          break;
  142.       }
  143.    }
  144.  
  145.    setvattrib(DIS_NORM);
  146.  
  147.    /* convert ascii date/times to file date/time formats */
  148.  
  149.    if (!quit)
  150.       for (i = 0; i < 4; i++) {
  151.          *cvtflds[i].cvtresult = (*cvtflds[i].cvtrtn)(cvtflds[i].p1,
  152.                                                       cvtflds[i].p2,&cvterr);
  153.          if (cvterr) {
  154.             show_error(0,0,2,"Invalid ",cvtflds[i].name);
  155.             quit = 1;
  156.          }
  157.       }
  158.  
  159.    /* get rid of the display box */
  160.  
  161.    popdwn(TDFROW,TDFCOL,TDROWS,TDCOLS,boxsave);
  162.    free(boxsave);
  163.  
  164.    /* finally tag the files */
  165.  
  166.    if (!quit)
  167.       tag_files(by_date);
  168.  
  169. }
  170.  
  171.  
  172. /******************************************************************************
  173.  **                         T A G _ I N V E R T                              **
  174.  *****************************************************************************/
  175.  
  176. tag_invert() {         /* invert the tag state of all files */
  177.  
  178.    register int i;
  179.    register struct file_ent *fp;
  180.  
  181.    fp = files;
  182.    for (i = 0; i < cw.nfiles; i++, fp++)
  183.       tag_toggle(fp,i);
  184.  
  185.    disp_file_stats();                          /* disp updated stats */
  186.  
  187. }
  188.  
  189.  
  190. /******************************************************************************
  191.  **                         T A G _ R E S E T                                **
  192.  *****************************************************************************/
  193.  
  194. tag_reset() {          /* clear all tagged file indicators */
  195.  
  196.    register int i;
  197.    register struct file_ent *fp;
  198.  
  199.    fp = files;
  200.    for (i = 0; i < cw.nfiles; i++, fp++)
  201.       if (fp->flags & TAGGED) {
  202.          fp->flags &= ~TAGGED;
  203.          if (on_screen(i)) {
  204.             gotorc(idx2sr(i),idx2sc(i));
  205.             disp_file(fp,i == cw.curidx);
  206.          }
  207.       }
  208.  
  209.    cw.num_tagged = 0;                          /* no files tagged now */
  210.    cw.tag_size = 0;
  211.    disp_file_stats();                          /* disp updated (0) stats */
  212.  
  213. }
  214.  
  215.  
  216. /******************************************************************************
  217.  **                         T A G _ T O D A Y                                **
  218.  *****************************************************************************/
  219.  
  220. tag_today() {          /* tag all files created/modified today */
  221.  
  222.    from_date = to_date = getdate();
  223.    from_time = 0;
  224.    to_time = 0xffff;
  225.  
  226.    tag_files(by_date);
  227. }
  228.  
  229.  
  230. /******************************************************************************
  231.                           T A G _ F I N D
  232.  *****************************************************************************/
  233.  
  234. tag_find(dir)          /* find (goto) the next or perv tagged file */
  235. int dir;
  236. {
  237.    register int i;
  238.  
  239.    if (cw.num_tagged == 0)             /* nowhere to go if no files tagged */
  240.       return;
  241.  
  242.    /* find the next (prev) tagged file */
  243.  
  244.    i = cw.curidx;
  245.    do {
  246.       i += dir;                        /* move forward or backward */
  247.       if (i >= cw.nfiles)              /* gotta watch for wrap arounds */
  248.          i = 0;
  249.       else
  250.          if (i < 0)
  251.             i = cw.nfiles - 1;
  252.    } while ((files[i].flags & TAGGED) == 0);   /* done if at a tagged file */
  253.  
  254.    /* figure out how to move the file pointer to the tagged file */
  255.  
  256.    if (on_screen(i)) {
  257.       fp_off(cw.curidx);               /* if the new file is currently */
  258.       fp_on(cw.curidx = i);            /*   displayed, just move the pointer */
  259.    } else {
  260.       cw.curidx = i;                   /* otherwise ... */
  261.       adjust_window();                 /* update entire window if the */
  262.       update_window(1);                /*   file isn't displayed */
  263.    }
  264. }
  265.  
  266.  
  267. /******************************************************************************
  268.  **                       T A G _ T O G G L E                                **
  269.  *****************************************************************************/
  270.  
  271. static int
  272. tag_toggle(fp,i)       /* toggle the tag state of file pointed to by fp */
  273. register int i;
  274. register struct file_ent *fp;
  275. {
  276.  
  277.    if (!(fp->flags & DIR)) {
  278.       if ((fp->flags ^= TAGGED) & TAGGED) {    /* toggle tag state */
  279.          cw.num_tagged++;                      /* keep track of # tagged */
  280.          cw.tag_size += fp->size;              /* and tagged size */
  281.       } else {
  282.          cw.num_tagged--;
  283.          cw.tag_size -= fp->size;
  284.       }
  285.       if (on_screen(i)) {                      /* display file ent with */
  286.          gotorc(idx2sr(i),idx2sc(i));          /* new display attribute */
  287.          disp_file(fp,i == cw.curidx);
  288.       }
  289.    }
  290. }
  291.  
  292.  
  293. /******************************************************************************
  294.  **                         T A G _ F I L E S                                **
  295.  *****************************************************************************/
  296.  
  297. static int
  298. tag_files(cmpfunc)     /* tag all files matching criteria */
  299. int (*cmpfunc)();
  300. {
  301.  
  302.    register int i;
  303.    register struct file_ent *fp;
  304.    int last_tagged = cw.num_tagged;
  305.  
  306.    /* scan the files[] structure looking for files to tag.  Don't tag empty
  307.       files[] entries, directory entries, already tagged entries, or entries
  308.       that don't meet the qualifications (as determined by cmpfunc()).  When
  309.       an entry is tagged, redisplay it if its currently displayed, update
  310.       num_tagged count, and tagged file size.  Update file status display
  311.       when done. */
  312.  
  313.    fp = files;
  314.    for (i = 0; i < cw.nfiles; i++, fp++)
  315.       if (!(fp->flags & DIR) && !(fp->flags & TAGGED) && (*cmpfunc)(fp)) {
  316.          fp->flags |= TAGGED;
  317.          if (on_screen(i)) {
  318.             gotorc(idx2sr(i),idx2sc(i));
  319.             disp_file(fp,i == cw.curidx);
  320.          }
  321.          cw.num_tagged++;
  322.          cw.tag_size += fp->size;
  323.       }
  324.  
  325.    if (cw.num_tagged != last_tagged)           /* actually tag any? */
  326.       disp_file_stats();                       /* disp updated stats */
  327. }
  328.  
  329.  
  330. /******************************************************************************
  331.  **                         B Y _ A T T R I B                                **
  332.  *****************************************************************************/
  333.  
  334. static int
  335. by_attrib(fp)          /* determine if file should be tagged by attribute */
  336. struct file_ent *fp;
  337. {
  338.    return(fp->flags & attrib);
  339. }
  340.  
  341.  
  342. /******************************************************************************
  343.  **                         B Y _ N A M E                                    **
  344.  *****************************************************************************/
  345.  
  346. static int
  347. by_name(fp)            /* determine if file entry should be tagged by name */
  348. struct file_ent *fp;
  349. {
  350.    return(match_name(fp->name,name));
  351. }
  352.  
  353.  
  354. /******************************************************************************
  355.  **                         B Y _ D A T E                                    **
  356.  *****************************************************************************/
  357.  
  358. static int
  359. by_date(fp)            /* determine if file entry should be tagged by date */
  360. register struct file_ent *fp;
  361. {
  362.    if ((fp->date > from_date ||
  363.        (fp->date == from_date && fp->time >= from_time)) &&
  364.        (fp->date < to_date   ||
  365.        (fp->date == to_date && fp->time <= to_time)))
  366.       return(1);
  367.    else
  368.       return(0);
  369. }
  370.  
  371.  
  372. /*****************************************************************************
  373.                         C V T D A T E / T I M E
  374.  *****************************************************************************/
  375.  
  376. static unsigned int
  377. cvtdate(date,dummy,ep) /* convert ascii date to integer file fmt */
  378. char *date, *dummy;
  379. int *ep;
  380. {
  381.    int month, day, year;
  382.  
  383.    *ep = 0;                            /* assume no error */
  384.  
  385.    month = cvttwo(date);               /* cvt flds to binary */
  386.    day   = cvttwo(date+3);
  387.    year  = cvttwo(date+6);
  388.  
  389.    /* error check */
  390.  
  391.    if (month < 1 || month > 12 || day < 1 || day > 31 || year < 80 ||
  392.        year > 99 || date[2] != '/' || date[5] != '/') {
  393.       *ep = 1;
  394.       return(0);
  395.    }
  396.  
  397.    /* turn it into the integer format */
  398.  
  399.    return( ((year - 80) << 9) + (month << 5) + day );
  400. }
  401.  
  402.  
  403. static unsigned int
  404. cvttime(time,ampmp,ep) /* convert ascii time to integer file fmt */
  405. char *time, *ampmp;
  406. int *ep;
  407. {
  408.    unsigned int hh, mm, ss, ampm;
  409.  
  410.    *ep = 0;                            /* assume no error */
  411.  
  412.    hh = cvttwo(time);                  /* cvt flds to binary */
  413.    mm = cvttwo(time+3);
  414.    ss = cvttwo(time+6);
  415.  
  416.    ampm = toupper(*ampmp);             /* make sure its upper case */
  417.  
  418.    /* error check */
  419.  
  420.    if (hh > 12 || mm > 59 || ss > 59 || time[2] != ':' || time[5] != ':' ||
  421.        (ampm != 'A' &&  ampm != 'P')) {
  422.       *ep = 1;
  423.       return(0);
  424.    }
  425.  
  426.    /* turn it into the integer format */
  427.  
  428.    if (ampm == 'P')
  429.       hh += 12;
  430.  
  431.    return( (hh << 11) + (mm << 5) + (ss >> 1) );
  432. }
  433.  
  434.  
  435. static int
  436. cvttwo(cp)             /* convert 2 ascii chars to binary */
  437. register char *cp;
  438. {
  439.    if (*cp == ' ')             /* it may have a leading blank */
  440.       *cp = '0';
  441.  
  442.    return( ((*cp - '0') * 10) + (*++cp - '0') );
  443. }
  444.